import server from '@/utils/server'
import ServerConstants from '@/utils/ServerConstants'


export function addOrder (params, success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }
  return new Promise((resolve, reject) => {
    server.postJSON(ServerConstants.REQ_USER_ADD_ORDER,
      {data: server.encode(params)},
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}


export function getOrderList (params, success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }
  return new Promise((resolve, reject) => {
    server.getJSON(ServerConstants.REQ_USER_GET_ORDER_LIST,
      params,
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}


export function getOrderDetail (params, success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }
  return new Promise((resolve, reject) => {
    server.getJSON(ServerConstants.REQ_USER_GET_ORDER_DETAIL,
      params,
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}

export function getPayWay (success, errorfun) {
  return new Promise((resolve, reject) => {
    server.getJSON(ServerConstants.REQ_USER_GET_PAY_ORDER,
      {},
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}

export function wxPay (params,success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }
  return new Promise((resolve, reject) => {
    server.getJSON(ServerConstants.REQ_USER_GET_WX_PAY,
      params,
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}


export function cancelOrder (params, success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }
  return new Promise((resolve, reject) => {
    server.postJSON(ServerConstants.REQ_USER_CANCEL_ORDEL,
      params,
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}


export function delOrder (params, success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }
  return new Promise((resolve, reject) => {
    server.postJSON(ServerConstants.REQ_USER_DEL_ORDEL,
      params,
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}


export function wxPayRecharge (params,success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }
  return new Promise((resolve, reject) => {
    server.postJSON(ServerConstants.REQ_USER_WET_PAY_ORDER,
      params,
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}
